package com.seio.edc.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class AssitDateUtil {

	public static Date getAddMinute(Date date, int amount) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.MINUTE, amount);
		return rightNow.getTime();
	}

	public static Date getAddHour(Date date, int amount) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.HOUR_OF_DAY, amount);
		return rightNow.getTime();
	}

	public static Date getAddDay(Date date, int amount) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.DAY_OF_MONTH, amount);
		return rightNow.getTime();
	}

	public static Date getAddMonth(Date date, int amount) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.MONTH, amount);
		return rightNow.getTime();
	}

	// 确定给定的时间是否在参考时间的误差范围之内
	public static boolean dateInField(Date date, Date compare) {
		return date.before(getAddMinute(compare, 2))
				&& date.after(getAddMinute(compare, -2));
	}

	// 获取两个时间间隔的分钟数
	public static int getSpanMinute(Date startDate, Date endDate) {
		return (int) ((endDate.getTime() - startDate.getTime()) / 60 / 1000);
	}

	// 获取两个时间间隔的分钟数
	public static int getSpanMinute(String startDate, String endDate) {
		Date start;
		Date end;
		try {
			start = DateUtil.stringToDate(startDate,
					DateUtil.DEFAULT_DATETIME_FORMAT);
			end = DateUtil.stringToDate(endDate,
					DateUtil.DEFAULT_DATETIME_FORMAT);
			return (int) ((end.getTime() - start.getTime()) / 60 / 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
}
